package com.bcmusic.order;

import entity.IdWorker;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import tk.mybatis.spring.annotation.MapperScan;

/**
 * @ClassName : OrderApplication  //类名
 * @Description :   //描述
 * @Author : YG  //作者
 * @Date: 2020-10-05 20:01  //时间
 */

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.bcmusic.music.feign","com.bcmusic.pay.feign","com.bcmusic.authorization.feign"})
//其它微服务通过名称调用该微服务需要加上这个
@EnableDiscoveryClient
@MapperScan("com.bcmusic.order.dao")
public class    OrderApplication {

    @Autowired
    private Environment env;


    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }

    @Bean
    public IdWorker idWorker(){
        return new IdWorker(0,1) ;
    }


    /***
     * 创建DirectExchange交换机
     * @return
     */
    @Bean
    public DirectExchange basicExchange(){
        return new DirectExchange(env.getProperty("mq.pay.exchange.order"), true,false);
    }


    /**
     * 等待队列
     * @return
     */
    @Bean
    public Queue orderTimerQueue() {
        return new Queue(env.getProperty("mq.pay.queue.ordertimer"), true);
    }

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue delayOrderTimerQueue() {
        return QueueBuilder.durable(env.getProperty("mq.pay.queue.ordertimerdelay"))
                .withArgument("x-dead-letter-exchange", env.getProperty("mq.pay.exchange.order"))        // 消息超时进入死信队列，绑定死信队列交换机
                .withArgument("x-dead-letter-routing-key", env.getProperty("mq.pay.queue.ordertimer"))   // 绑定指定的routing-key
                .build();
    }

    /***
     * 交换机与队列绑定
     * @return
     */
    @Bean
    public Binding basicBinding() {
        return BindingBuilder.bind(orderTimerQueue())
                .to(basicExchange())
                .with(env.getProperty("mq.pay.queue.ordertimer"));
    }


}
